package com.itxiuyixiu.homework.entity.dao;

import com.itxiuyixiu.homework.entity.model.Course;
import com.itxiuyixiu.homework.entity.model.CourseRecord;
import com.itxiuyixiu.homework.entity.vo.CourseRecordVo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.Null;
import java.util.List;
import java.util.Optional;

/**
 * @author 黄磊
 * @date 2020/3/5
 */
public interface CourseRecordDao extends JpaRepository<CourseRecord, Integer> {
    boolean existsByStudentStudentId(String studentId);

    boolean existsByCourseId(int courseId);

    void deleteByStudentStudentIdAndId(String studentId, int courseId);

    Optional<CourseRecord> findByStudentStudentIdAndCourseId(String studentId , int courseId);

    @Transactional
    @Query(value = "select id, gmtCreated, gmtModified, makeupScore, rebuild, score, selectTime, fk_course_id as courseId, fk_student_studentId as studentId from CourseRecord where id=?1" , nativeQuery = true)
    List<CourseRecordVo> findAllById(int id);

    @Transactional
    @Query(value = "select id, gmtCreated, gmtModified, makeupScore, rebuild, score, selectTime, fk_course_id as courseId, fk_student_studentId as studentId from CourseRecord where fk_course_id=?1" , nativeQuery = true)
    List<CourseRecordVo> findAllByCourseId(int courseId);

    @Transactional
    @Query(value = "select id, gmtCreated, gmtModified, makeupScore, rebuild, score, selectTime, fk_course_id as courseId, fk_student_studentId as studentId from CourseRecord where fk_student_studentId=?1" , nativeQuery = true)
    List<CourseRecordVo> findAllByStudentId(String studentId);

    @Transactional
    @Query(value = "select id, gmtCreated, gmtModified, makeupScore, rebuild, score, selectTime, fk_course_id as courseId, fk_student_studentId as studentId from CourseRecord" , nativeQuery = true)
    List<CourseRecordVo> findAllCourseRecord();
}
